using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace de_kleintjesmarkt
{
    public partial class CtlRegistreren : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void vldTxtPassword1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            // Password validation is enabled on the second password field
            // so we'll check if the first password field's value is the same
            // as the second field's value.
            if (txtWachtwoord.Text != args.Value)
            {
                args.IsValid = false;
                return;
            }

            args.IsValid = true;
            return;
        }

        protected void btRegistreer_Click(object sender, EventArgs e)
        {
            string errorText = "";

            try
            {
               // If isHashed if still zero javascript is disabled. We'll hash
               // the password client-side.
               if ("0" == hfIsHashed.Value)
               {
                  txtWachtwoord.Text = ChallengeBuilder.createMD5(txtEmail.Text + txtWachtwoord.Text);
                  
               }

               // Retrieve form values
               string email = txtEmail.Text;
               string password = txtWachtwoord.Text;
               string firstname = txtVoornaam.Text;
               string lastname = txtNaam.Text;
               string name_addition = txtVoorletters.Text;
               string address = txtStraatnaam.Text;
               string house_number = txtHuisnummer.Text;
               string house_number_add = txtHuisnummerToevoeging.Text;
               string city = txtWoonplaats.Text;
               string postal_code = txtPostcodeNummers.Text + txtPostcodeLetters.Text;
               string member_number = txtAbonneeNummer.Text;

               RegisterUser.Klant k = new RegisterUser.Klant();

               k.Email = email;
               k.Firstname = firstname;
               k.Lastname = lastname;
               k.Name_addition = name_addition;
               k.Address = address;
               k.House_number = Int32.Parse( house_number );
               k.House_number_add =  house_number_add;
               k.City = city;
               k.Postal_code = postal_code;
               k.Member_number = Int32.Parse(member_number);

               // Jeweetzelluf, stop die data in die sessie, jeweet.
               Session["klant"] = k;

               // Register the user using the userRegister web service.
               RegisterUser.Registreren register = new RegisterUser.Registreren();
               register.RegisterUser1(k, password);
               //register.RegisterUser(email, password, firstname, lastname, name_addition, address,
               //house_number, house_number_add, city, postal_code, member_number);
               
               //dsRegister.Insert();

               Response.Redirect("Login.aspx");
            }
            // If SqlExceptions turn up this will very probably be because the
            // username allready exists. This is also the warning we want to
            // present to the user.
            catch (System.Data.SqlClient.SqlException ex)
            {
               errorText = "Het door u gekozen e-mail adres is al in gebruik.";
            }
            finally
            {
               txtErrors.Text = errorText;
            }
        }
    }
}